package com.limao.servlet;

import javax.servlet.AsyncContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONException;
import com.limao.CacheManager.AddressInfo;
import com.limao.CacheManager.AdvInfo;
import com.limao.CacheManager.CrowdCompanyInfo;
import com.limao.CacheManager.GoodsInfo;
import com.limao.CacheManager.OrderInfo;
import com.limao.CacheManager.RegLoginInfo;
import com.limao.CacheManager.SmallCompanyInfo;
import com.limao.CacheManager.UserInfo;
import com.limao.Model.LzPluginException;
import com.limao.Model.PublicData;
import com.limao.Model.RecvData;
import com.limao.Tool.CacheClass;
import com.limao.Tool.Function;
import com.limao.Utils.OutPrintClass;


public class PlugAsyncProcessor implements Runnable {

	private AsyncContext asyncContext;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private RecvData DataObj;

	public PlugAsyncProcessor() {
	}

	public PlugAsyncProcessor(AsyncContext asyncCtx, HttpServletRequest request,HttpServletResponse response,RecvData DataObj) {
		this.asyncContext = asyncCtx;
		this.request = request;
		this.response=response;
		this.DataObj=DataObj;
	}

	@Override
	public void run() {
		HttpSession session=request.getSession();
		
		int m = -1;
		Object reply = null;

		try {
			
			m = Integer.parseInt(DataObj.m);
			
			if(CacheClass.getIntValue(CacheClass.getSYS_BaseDataCacheManager().Load_Param("ispaused"))==1)
			{
				if(CacheClass.Maintenance_Start_time==0) 
					CacheClass.Maintenance_Start_time=CacheClass.getTime();
				
				int timel=CacheClass.getIntValue(CacheClass.getSYS_BaseDataCacheManager().Load_Param("pausedtime"));
				
				OutPrintClass.sendMaintenance(response, asyncContext, m, "系统维护中,维护开始时间："+CacheClass.getFormatDate(CacheClass.Maintenance_Start_time,"yyyy-MM-dd HH:mm:ss")+"，维护时长约"+timel+"分钟");
				return ;
			}
			else
				CacheClass.Maintenance_Start_time=0;
			
			String userid=CacheClass.isLogin(request,response);
			
			if(CacheClass.isEmpty(userid))
			{
				PublicData jsonObj = new PublicData();
				jsonObj.m = m+"";
				jsonObj.state = "100";
				jsonObj.info = "您还未登陆";
				OutPrintClass.writer(response, jsonObj,asyncContext);
				return;
			}


			
			if(CacheClass.getIntValue(CacheClass.getSYS_BaseDataCacheManager().Load_Param("ispaused"))==1)
			{
				OutPrintClass.sendError(response, asyncContext, m, "系统维护中");
				return ;
			}
			
			if (CacheClass.isEmpty(DataObj.userid))
			{
				DataObj.userid=userid;
			}
			
			switch (m) 
			{
			    //&新鲜宝  修改密码(登陆后)  32
				case Function.UPDATA_OPERATION_PASSWORD_3:// 修改密码(登陆后)
					reply= RegLoginInfo.setNewPassword_afterLogin(response,asyncContext, m, DataObj);
					break;	
				//&新鲜宝  安全退出登陆(登陆后)	61
				case Function.LOGINOUT_OPERATION_1: 
					String selfSessionId=CacheClass.getCookieSession(request);
					reply= RegLoginInfo.loginout(session,selfSessionId, response,asyncContext, m, DataObj);
					break;					
				//&新鲜宝 　用户信息     200
				case Function.LOAD_USERINFO_RMB: 
					reply = UserInfo.getLkAuthInfo(response,asyncContext,m, DataObj);
					break;
				//&新鲜宝  修改本人的呢称   114
				case Function.SET_USERINFO_OPERATION_NAME: 
					reply = UserInfo.SetAuthName(response,asyncContext,m, DataObj);
					break;
					
				//&新鲜宝 修改本人的头像     115
				case Function.SET_USERINFO_OPERATION_AuthAvatar: 
					reply = UserInfo.SetAuth_AvatarInfo(response,asyncContext,m, DataObj);
					break;
				//&新鲜宝 修改本人的基本信息   116
				case Function.SET_USERINFO_OPERATION_AuthAndBase: //新鲜宝 修改本人的基本信息
					reply = UserInfo.SetAuth_BaseInfo(response,asyncContext,m, DataObj);
					break;
					
				//&新鲜宝 修改本人提现绑定   202
				case Function.UPDATE_USERINFO_OPERATION_BANK: //新鲜宝 修改本人的提现绑定
					reply =  UserInfo.SetAuth_BankInfo(response,asyncContext,m, DataObj);
					break;
						
				
				//&新鲜宝 开通交易帐户   205
				case Function.OPEN_USERINFO_TRADE_USER: //开通交易帐户
					reply =  UserInfo.OPEN_USERINFO_TRADE_USER(response,asyncContext,m, DataObj);
					break;		
					
					
					
				// **********************************************************************************常用地址
				//&新鲜宝 返回某人默认     146
				case Function.LOAD_USERINFO_OPERATION_ADDRESS_USERDEF:
					reply =  AddressInfo.load_User_Address_UserDef(response,asyncContext,m, DataObj);
					break;
					
				//&新鲜宝 众筹支持（购买）   正式订单生成    1720
				case Function.SET_ORDER_OPERATION_NEW:
					reply =  OrderInfo.resultSYSAddOrder_ByShopcart(request,response,asyncContext,m, DataObj);
					break;	
				//&新鲜宝  用户订单取消     1721
				case Function.SET_ORDER_OPERATION_CANCEL:
					reply =  OrderInfo.resultDelete_Order_ByUser(response,asyncContext,m, DataObj);
					break;	
				//&新鲜宝    用户对未付款(或付款失败)订单，再次付款     1722
				case Function.SET_ORDER_OPERATION_REPAY:
					reply =  OrderInfo.resultRepay_Order_ByUser(request,response,asyncContext,m, DataObj);
					break;
					
				//&新鲜宝  取用户订单列表（所有）    1750
				case Function.GET_ORDER_OPERATION_LIST:
					reply =  OrderInfo.load_OrderByUser_id(response,asyncContext,m, DataObj);
					break;
					
				//&新鲜宝 取用户某订单详细信息    1755
				case Function.GET_ORDER_OPERATION_INFO:
					reply =  OrderInfo.resultSYSload_OrderById(response,asyncContext,m, DataObj);
					break;
					
				//&新鲜宝 确认收货   1617
				case Function.SET_SHOP_OPERATION_SCORE:
					reply =  OrderInfo.resultSYSOrderScore(response,asyncContext,m, DataObj);
					break;			
		
				//&新鲜宝     取用户收益（余额）统计列表     1780
				case Function.GET_RMB_LIST:
					reply =  UserInfo.load_RMB_List(response,asyncContext,m, DataObj);
					break;

					
				//新鲜宝    取资产列表    1782
				case Function.GET_RMB_LIST3:
					reply =  UserInfo.load_RMB_List3(response,asyncContext,m, DataObj);
					break;					
					
				//&新鲜宝 取用户团队列表    1785
				case Function.GET_MANS_LIST:   
					reply =  UserInfo.load_MANS_LIST(response,asyncContext,m, DataObj);
					break;		
					
				case Function.SET_MT_TOKEN:   //1788
					reply = UserInfo.SET_MT_TOKEN(response,asyncContext,m, DataObj);
					break;					
					
				//&新鲜宝 提现    215
				case Function.GET_RMB_Withdraw :
					reply =  UserInfo.RMB_Withdraw(response,asyncContext,m, DataObj);
					break;
				//&新鲜宝 提现列表   216
				case Function.GET_RMB_WithdrawLIST :
					reply =  UserInfo.GET_RMB_WithdrawLIST(response,asyncContext,m, DataObj);
					break;						
				//&新鲜宝 提现详情     217
				case Function.GET_RMB_WithdrawDETAIL :
					reply =  UserInfo.GET_RMB_WithdrawDETAIL(response,asyncContext,m, DataObj);
					break;	
					
				//&新鲜宝  手机通讯录手机注册与否   223
				case Function.GET_MOBILESTATE:
					reply = AdvInfo.getMobileState(response,asyncContext,m, DataObj);
					break;	
				//&新鲜宝 短信推广文字    224
				case Function.GET_MESTXT:
					reply = AdvInfo.getMobileMsgtxt(response,asyncContext,m, DataObj);
					break;	
	
					
				//&新鲜宝	市场交易首页	240
				case Function.GET_VRHomeContent:
					reply = AdvInfo.GET_VRHomeContent(response,asyncContext,m, DataObj);
					break;
					
				//&新鲜宝	市场交易帐户开启	241
				case Function.GET_VRHomeOpenUser:
					reply = UserInfo.GET_VRHomeOpenUser(response,asyncContext,m, DataObj);
					break;	
				//&新鲜宝	转入资金取信息	242
				case Function.GET_VRPUTINRMBGetUser:
					reply = UserInfo.GET_VRPUTINRMBGetUser(response,asyncContext,m, DataObj);
					break;						
						
				//&新鲜宝 开店申请(或 开店被拒，修改后再次提交) 1610
				case Function.SET_SHOP_OPERATION_NEW:
					reply = CrowdCompanyInfo.SET_SHOP_OPERATION_NEW(response, asyncContext,
							m, DataObj);
					break;	
				//&新鲜宝 发表话题或回复 1640
				case Function.GET_SHOP_OPERATION_DISCUSS:
					reply = GoodsInfo.resultSYSGoodsDISCUSSInsert(response, asyncContext,m, DataObj);
					break;						

				//办事处管理   取办事处详情 1650	
				case Function.GET_SMALLCOMPANY_OPERATION_DETAIL:
					reply = SmallCompanyInfo.GET_SMALLCOMPANY_OPERATION_DETAIL(response, asyncContext,m, DataObj);
					break;						
				//办事处管理   取办事处列表 1651	
				case Function.GET_SMALLCOMPANY_OPERATION_LIST:
					reply = SmallCompanyInfo.GET_SMALLCOMPANY_OPERATION_LIST(response, asyncContext,m, DataObj);
					break;						
				//办事处管理   添加办事处 1652	
				case Function.ADD_SMALLCOMPANY_OPERATION_INFO:
					reply = SmallCompanyInfo.ADD_SMALLCOMPANY_OPERATION_INFO(response, asyncContext,m, DataObj);
					break;	
				//办事处管理   编辑办事处 1653	
				case Function.EDIT_SMALLCOMPANY_OPERATION_INFO:
					reply = SmallCompanyInfo.EDIT_SMALLCOMPANY_OPERATION_INFO(response, asyncContext,m, DataObj);
					break;						
				//办事处管理   取办事处结算记录 1654	
				case Function.GET_SMALLCOMPANY_OPERATION_DRAWLIST:
					reply = SmallCompanyInfo.GET_SMALLCOMPANY_OPERATION_DRAWLIST(response, asyncContext,m, DataObj);
					break;					
					
				//活跃　获得活跃记录	
				case Function.GET_SHOP_OPERATION_PRESTIGE_LOG:
					reply = GoodsInfo.GET_SHOP_OPERATION_PRESTIGE_LOG(response, asyncContext,m, DataObj);
					break;	
					
				//活跃商城商品列表	
				case Function.GET_SHOP_OPERATION_PRESTIGE:
					reply = GoodsInfo.GET_SHOP_OPERATION_PRESTIGE(response, asyncContext,m, DataObj);
					break;
				//活跃商城的商品详情	
				case Function.GET_SHOP_OPERATION_PRESTIGE_GOODS_DETAIL:
					reply = GoodsInfo.GET_SHOP_OPERATION_PRESTIGE_GOODS_DETAIL(response, asyncContext,m, DataObj);
					break;					
				//领取活跃商城商品	
				case Function.GET_SHOP_OPERATION_PRESTIGE_GOODS:
					reply = GoodsInfo.GET_SHOP_OPERATION_PRESTIGE_GOODS(response, asyncContext,m, DataObj);
					break;					
				//活跃商城的订单列表
				case Function.GET_SHOP_OPERATION_PRESTIGE_ORDER:
					reply = GoodsInfo.GET_SHOP_OPERATION_PRESTIGE_ORDER(response, asyncContext,m, DataObj);
					break;
				//活跃商城的订单 　申请发货
				case Function.GET_SHOP_OPERATION_PRESTIGE_ORDER10:
					reply = GoodsInfo.GET_SHOP_OPERATION_PRESTIGE_ORDER10(response, asyncContext,m, DataObj);
					break;
				//活跃商城的订单　回购操作	
				case Function.GET_SHOP_OPERATION_PRESTIGE_ORDER11:
					reply = GoodsInfo.GET_SHOP_OPERATION_PRESTIGE_ORDER11(response, asyncContext,m, DataObj);
					break;
				//活跃商城的订单详情	
				case Function.GET_SHOP_OPERATION_PRESTIGE_ORDER_DETAIL:
					reply = GoodsInfo.GET_SHOP_OPERATION_PRESTIGE_ORDER_DETAIL(response, asyncContext,m, DataObj);
					break;
						
					
				//虚拟币变动记录	
				case Function.GET_SHOP_OPERATION_PRESTIGE_Vr_LOG:
					reply = GoodsInfo.GET_SHOP_OPERATION_PRESTIGE_Vr_LOG(response, asyncContext,m, DataObj);
					break;						
				//虚拟币商城商品列表	
				case Function.GET_SHOP_OPERATION_PRESTIGE_Vr:
					reply = GoodsInfo.GET_SHOP_OPERATION_Vr_PRESTIGE(response, asyncContext,m, DataObj);
					break;	
				//虚拟币商城的商品详情	
				case Function.GET_SHOP_OPERATION_PRESTIGE_Vr_GOODS_DETAIL:
					reply = GoodsInfo.GET_SHOP_OPERATION_PRESTIGE_Vr_GOODS_DETAIL(response, asyncContext,m, DataObj);
					break;						
					
				//购买虚拟币商城商品	
				case Function.GET_SHOP_OPERATION_PRESTIGE_Vr_GOODS:
					reply = GoodsInfo.GET_SHOP_OPERATION_PRESTIGE_Vr_GOODS(response, asyncContext,m, DataObj);
					break;					
				//虚拟币商城的订单列表
				case Function.GET_SHOP_OPERATION_PRESTIGE_Vr_ORDER:
					reply = GoodsInfo.GET_SHOP_OPERATION_PRESTIGE_Vr_ORDER(response, asyncContext,m, DataObj);
					break;
				//虚拟币商城订单 　申请发货
				case Function.GET_SHOP_OPERATION_PRESTIGE_Vr_ORDER10:
					reply = GoodsInfo.GET_SHOP_OPERATION_PRESTIGE_Vr_ORDER10(response, asyncContext,m, DataObj);
					break;
				//虚拟币商城订单　回购操作	
				case Function.GET_SHOP_OPERATION_PRESTIGE_Vr_ORDER11:
					reply = GoodsInfo.GET_SHOP_OPERATION_PRESTIGE_Vr_ORDER11(response, asyncContext,m, DataObj);
					break;					
				//虚拟币商城的订单详情	
				case Function.GET_SHOP_OPERATION_PRESTIGE_Vr_ORDER_DETAIL:
					reply = GoodsInfo.GET_SHOP_OPERATION_PRESTIGE_Vr_ORDER_DETAIL(response, asyncContext,m, DataObj);
					break;	
	
				//&新鲜宝     取用户现货瓶数记录列表     1787
				case Function.GET_CREDIT_LIST:
					reply =  UserInfo.load_RMB_List2(response,asyncContext,m, DataObj);
					break;	
					
				//&新鲜宝     取用户宝箱列表     1680	
				case Function.GET_LIST_OPERATION_REDBAG:
					reply =  UserInfo.GET_LIST_OPERATION_REDBAG(response,asyncContext,m, DataObj);
					break;	
				//&新鲜宝     用户打开宝箱     1681	
				case Function.OPEN_OPERATION_REDBAG:
					reply =  UserInfo.OPEN_OPERATION_REDBAG(response,asyncContext,m, DataObj);
					break;						
					
				//用户发布寄售    3010	
				case Function.SET_USERSHOP_SELLBUY_OPERATION_SELL:
					reply =  UserInfo.SET_USERSHOP_SELLBUY_OPERATION_SELL(response,asyncContext,m, DataObj);
					break;						
				//用户查看寄售列表    3020	
				case Function.GET_USERSHOP_SELLBUY_OPERATION_LIST:
					reply =  UserInfo.GET_USERSHOP_SELLBUY_OPERATION_LIST(response,asyncContext,m, DataObj);
					break;	
				//用户购买寄售    3030	
				case Function.GET_USERSHOP_SELLBUY_OPERATION_BUY:
					reply =  UserInfo.GET_USERSHOP_SELLBUY_OPERATION_BUY(response,asyncContext,m, DataObj);
					break;
				//取寄售卖买前5    3040	
				case Function.GET_USERSHOP_SELLBUY_OPERATION_RANK5:
					reply =  UserInfo.GET_USERSHOP_SELLBUY_OPERATION_RANK5(response,asyncContext,m, DataObj);
					break;				
				//取寄售近30天日成交均价    3050	
				case Function.GET_USERSHOP_SELLBUY_OPERATION_30DAYS_PRICE:
					reply =  UserInfo.GET_USERSHOP_SELLBUY_OPERATION_30DAYS_PRICE(response,asyncContext,m, DataObj);
					break;					

					
				//酒交所开户（转发）    3310	
				case Function.USER_TRADESYS_OPERATION_REG:
					reply =  UserInfo.USER_TRADESYS_OPERATION_REG(response,asyncContext,m, DataObj);
					break;	
				//酒交所开户（查询开户审核进度）    3320	
				case Function.USER_TRADESYS_OPERATION_REGING:
					reply =  UserInfo.USER_TRADESYS_OPERATION_REGING(response,asyncContext,m, DataObj);
					break;							
				//酒交所转入瓶数    3330	
				case Function.USER_TRADESYS_OPERATION_IN_HOLD:
					reply =  UserInfo.USER_TRADESYS_OPERATION_IN_HOLD(response,asyncContext,m, DataObj);
					break;	
				//现货列表 （含用数据瓶数）   3340	
				case Function.USER_TRADESYS_OPERATION_VRGOODS_List:
					reply =  UserInfo.USER_TRADESYS_OPERATION_VRGOODS_List(response,asyncContext,m, DataObj);
					break;
				//现货 　申请发货  3350
				case Function.USER_TRADESYS_OPERATION_VRORDER:
					reply = UserInfo.USER_TRADESYS_OPERATION_VRORDER(response, asyncContext,m, DataObj);
					break;
					
					
				//实物奖品的订单列表  1685
				case Function.GET_SHOP_OPERATION_REDBAG_ORDER:
					reply = GoodsInfo.GET_SHOP_OPERATION_REDBAG_ORDER(response, asyncContext,m, DataObj);
					break;
				//实物奖品订单 　申请发货  1686
				case Function.GET_SHOP_OPERATION_REDBAG_ORDER10:
					reply = GoodsInfo.GET_SHOP_OPERATION_REDBAG_ORDER10(response, asyncContext,m, DataObj);
					break;
				//实物奖品订单　回购操作	 1687
				case Function.GET_SHOP_OPERATION_REDBAG_ORDER11:
					reply = GoodsInfo.GET_SHOP_OPERATION_REDBAG_ORDER11(response, asyncContext,m, DataObj);
					break;
				//实物奖品订单详情	 1689
				case Function.GET_SHOP_OPERATION_REDBAG_ORDER_DETAIL:
					reply = GoodsInfo.GET_SHOP_OPERATION_REDBAG_ORDER_DETAIL(response, asyncContext,m, DataObj);
					break;	
						
					
					
						
				default:
					reply = OutPrintClass.errorObj(m,"出错，功能不存--P");
		
			}
			

		} catch (NumberFormatException e) {
			reply = OutPrintClass.errorObj(m,  "出错，数据类型出错");
			e.printStackTrace();
		} catch (ClassCastException e) {
			reply = OutPrintClass.errorObj(m, "出错，数据类型出错");
			e.printStackTrace();
		} catch (JSONException e) {
			reply = OutPrintClass.errorObj(m,  "出错，JSON数据错误");
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			reply = OutPrintClass.errorObj(m, "出错，数据项不符合要求");
			e.printStackTrace();
		} catch (LzPluginException e) {
			reply = OutPrintClass.errorObj(m,e.getMessage());
			e.printStackTrace();
		} catch (UnsupportedOperationException e) {
			reply = OutPrintClass.errorObj(m, "出错，数据项为只读");
			e.printStackTrace();
		} catch (Exception e) {
			reply = OutPrintClass.errorObj(m, "出错，未知错误:P");
			e.printStackTrace();
		}
			
		
		OutPrintClass.writer(response, reply, asyncContext);

	}

	
}
